package server

import (
	"os"
	"singo/api"
	"singo/middleware"

	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

// NewRouter 路由配置
func NewRouter() *gin.Engine {
	r := gin.Default()
	if gin.Mode() == gin.ReleaseMode {
		url := ginSwagger.URL("https://xd.inpm.top/swagger/doc.json")
		r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
	} else {
		url := ginSwagger.URL("http://localhost:3001/swagger/doc.json")
		r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
	}
	// r.StaticFile("/swagger.json", "./swagger/swagger.json")
	// r.Static("/swagger", "./swagger/dist")

	// 中间件, 顺序不能改
	r.Use(middleware.Session(os.Getenv("SESSION_SECRET")))
	r.Use(middleware.Cors())
	r.Use(middleware.CurrentUser())

	// 路由
	v1 := r.Group("/api/v1")
	{
		v1.POST("ping", api.Ping)

		// 用户相关
		v1.POST("user/register", api.UserRegister)
		v1.POST("user/login", api.UserLogin)

		// 上传文件
		v1.POST("/upload/file", api.UploadFile)

		// 版本记录
		v1.POST("version/create", api.VersionCreate)

		// 错误日志CURD
		v1.POST("mistake/create", api.MistakeCreate)
		v1.GET("mistakes", api.MistakeList)
		v1.DELETE("mistake/:id", api.MistakeDelete)
		v1.PUT("mistake/update", api.MistakeUpdate)
		v1.GET("mistake/:id", api.MistakeShow)

		// 需要登录保护的
		auth := v1.Group("")
		auth.Use(middleware.AuthRequired())
		{
			// 用户相关
			auth.GET("users", api.UserList)
			auth.GET("user/info", api.UserInfo)
			auth.PUT("user/update", api.UserUpdate)
			auth.PUT("user/resetPassword/:id", api.ResetPassword)
			auth.PUT("user/changePassword", api.ChangePassword)
			auth.PUT("user/changeAvatar", api.ChangeAvatar)
			auth.DELETE("user/delete/:id", api.UserDelete)
			auth.DELETE("user/logout", api.UserLogout)
		}

		// 组织CURD
		auth.POST("organization/create", api.OrganizationCreate)
		auth.GET("organizations", api.OrganizationList)
		auth.DELETE("organization/:id", api.OrganizationDelete)
		auth.PUT("organization/update", api.OrganizationUpdate)
		auth.GET("organization/:id", api.OrganizationShow)

		// 任务CURD
		auth.POST("task/create", api.TaskCreate)
		auth.GET("tasks", api.TaskList)
		auth.DELETE("task/:id", api.TaskDelete)
		auth.PUT("task/update", api.TaskUpdate)
		auth.GET("task/:id", api.TaskShow)
		auth.PUT("task/check", api.TaskCheck)

		// 项目CURD
		auth.POST("project/create", api.ProjectCreate)
		auth.GET("projects", api.ProjectList)
		auth.DELETE("project/:id", api.ProjectDelete)
		auth.PUT("project/update", api.ProjectUpdate)
		auth.GET("project/:id", api.ProjectShow)

		// 需求CURD
		auth.POST("demand/create", api.DemandCreate)
		auth.GET("demands", api.DemandList)
		auth.DELETE("demand/:id", api.DemandDelete)
		auth.PUT("demand/update", api.DemandUpdate)
		auth.GET("demand/:id", api.DemandShow)

		// 缺陷CURD
		auth.POST("defect/create", api.DefectCreate)
		auth.GET("defects", api.DefectList)
		auth.DELETE("defect/:id", api.DefectDelete)
		auth.PUT("defect/update", api.DefectUpdate)
		auth.GET("defect/:id", api.DefectShow)

		// 管理
		auth.GET("manage/projects", api.ManageProjectList)
		auth.GET("manage/members", api.ManageMemberList)
		auth.GET("manage/summarys", api.ManageSummaryList)
		auth.GET("manage/task/checks", api.ManageTaskCheckList)

		// 绩效
		auth.GET("/myperfomance", api.MyPerformance)
		auth.GET("/perfomances", api.PerformanceList)
		auth.GET("/performance/rank", api.PerformanceRank)

	}
	return r
}
